This snippet allows the user to upload an image onto the server, check if the image is valid, and then resize the image to specified size. The image can be either outputted to the browser or saved to a file.

Requires GD library

==========================================================

/*
settings
*/
$maxsize=1048576;		//in bytes (default 1048576)

$perserve_ratio=true;	//if true prevents distortion

$max_width=200;			//in pixels, must be greater than 0

$max_height=200;		//in pixels, must be greater than 0

$output_type=0;			//determines type:
						//0 - jpg
						//1 - png
						//2 - gif
						//3 - same as uploaded file

$jpg_quality=100;		//if jpg selected, how good should the quality be (0 - 100)

$output_setting=0;		//0, outputs image to browser
						//1, outputs image to a file
						//2, outputs to file and browser

$file_dir_output="";	//if output to file, what directory to save (e.g images or pics/small)
						//no slash should be put at the end or beginning

$file_name_output="img";//if output to file, what name to save

$append_timestamp=false;//whether to add a timestamp to every image

$file_exist_option=0;	//if a file exist, what to do:
						//0 - append a number
						//1 - overwrite
						//2 - ignore

/*
end settings
*/

//the uploaded file
if (isset($_FILES["image"]))
{
	//check filesize if exists there's a file
	if ($_FILES['image']['size'] > 0)
	{
		//check to make sure that the file isn't too big
		if ($_FILES['image']['size'] > $maxsize)
		{
			die("File too big");
		}

		if ($_FILES['image']["name"]=="")
		{
			die ("File must have a name");
		}

		$elem=getimagesize($_FILES['image']['tmp_name']);
		$type=image_type_to_extension($elem[2],false);
		if (!($type=="png" || $type=="jpeg" || $type=="gif"))
		{
			die("Invalid filetype");
		}

		//determine type
		switch ($type)
		{
			case 'png':$new_ext="png";
			break;
			case 'jpeg':$new_ext="jpg";
			break;
			case 'gif':$new_ext="gif";
			break;
		}

		switch ($output_type)
		{
			case 0: $new_output_type="jpg";
			break;
			case 1: $new_output_type="png";
			break;
			case 2: $new_output_type="gif";
			break;
			default: $new_output_type=$new_ext;
			break;
		}

		$width=$elem['0'];
		$height=$elem['1'];

		//resize
		if ($width<"1" || $height<"1")
		{
			die("Image contained no data");
		}

		//convert size while saving ratio
		if (($width > $max_width) || ($height > $max_height))
		{
			if ($perserve_ratio==true)
			{
				if ($width > $height)
				{
					$new_width=$max_width;
					$new_height=(($max_height * $height) / $width);
				}
				else if ($height < $width)
				{
					$new_height=$max_height;
					$new_width=(($max_width * $width) / $height);
				}
				else
				{
					$new_width=$max_width;
					$new_height=$max_height;
				}
			}
			else
			{
				$new_width=$max_width;
				$new_height=$max_height;
			}

		}
		else
		{
			$new_width=$width;
			$new_height=$height;
		}

		
		//create image
		$thumb=imagecreatetruecolor($new_width, $new_height);
		if ($new_ext=="png")
		{
			$source=imagecreatefrompng($_FILES['image']['tmp_name']);
		}
		else if ($new_ext=="gif")
		{
			$source=imagecreatefromgif($_FILES['image']['tmp_name']);
		}
		else
		{
			$source=imagecreatefromjpeg($_FILES['image']['tmp_name']);
		}

		// Resize
		if (!imagecopyresampled($thumb, $source, 0, 0, 0, 0, $new_width, $new_height, $width, $height))
		{
			die("Error resizing image!");
		}

		//outputs to file if selected
		if ($output_setting==1 || $output_setting==2)
		{
			if ($file_dir_output!="")
			{
				if (!file_exists($file_dir_output."/"))
				{
					die ("Directory doesn't exist!");
				}
			}

			$add_num=0;
			$ignore=false;
			$append_name="";
			$append_num="";
			$new_dir_output="";

			//puts a timestamp on every name if selected
			if ($append_timestamp==true)
			{
				$append_name=date("mdyHis");
			}

			if ($file_dir_output!="")
			{
				$new_dir_output=$file_dir_output."/";
			}

			//checks if file exists
			if ($file_exist_option==0)
			{
				if (file_exists($new_dir_output.$file_name_output.$append_name.".".$new_output_type))
				{
					$add_num=1;

					while (file_exists($new_dir_output.$file_name_output.$append_name.$add_num.".".$new_output_type))
					{
						$add_num++;
					}
				}
			}
			else if ($file_exist_option==2)
			{
				if (file_exists($new_dir_output.$file_name_output.$append_name.".".$new_output_type))
				{
					$ignore=true;
				}
			}

			if ($ignore==false)
			{
				//adds a number if not 0
				if ($add_num!=0)
				{
					$append_num=$add_num;
				}

				switch ($new_output_type)
				{
					case 'jpg':
						imagejpeg($thumb,$new_dir_output.$file_name_output.$append_name.$append_num.".jpg",$jpg_quality);
					break;
					case 'png':
						imagepng($thumb,$new_dir_output.$file_name_output.$append_name.$append_num.".png");
					break;
					case 'gif':
						imagegif($thumb,$new_dir_output.$file_name_output.$append_name.$append_num.".gif");
					break;
				}
			}
		}

		//outputs to browser if selected
		if ($output_setting==0 || $output_setting==2)
		{
			switch ($new_output_type)
			{
				case 'jpg':
					header("Content-type: image/jpeg");
					imagejpeg($thumb,null,$jpg_quality);
				break;
				case 'png':
					header("Content-type: image/png");
					imagepng($thumb);
				break;
				case 'gif':
					header("Content-type: image/gif");
					imagegif($thumb);
				break;
			}
		}
	}
}